/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import CRUD.MemberHome;
import CRUD.PostHome;
import CRUD.ThreadHome;
import Utility.MemberAccountUtility;
import Utility.PostOperationsUtility;
import Utility.ThreadOperationsUtility;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import xmlMapping.Member;
import xmlMapping.Post;

/**
 *
 * @author Emyzzz
 */
public class PostAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element :
     * ( <action parameter="method" .../> )
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

        System.out.println("here iam in add method in post dispatch action");
        int threadId = ((beans.PostActionForm) form).getThreadid();
        int memberid = ((beans.PostActionForm) form).getMemberid();
        System.out.println(memberid);

        String original=((beans.PostActionForm) form).getPostBody();
        System.out.println(" the original post is "+original);
     
  
        System.out.println(" filtered string is "+original);
        Post newPost = new Post();
        newPost.setBody(original); //body
        newPost.setLastModified(sqlDate); //last mod data
        newPost.setRank(0); // rank
        newPost.setTitle(((beans.PostActionForm) form).getPostTitle()); //title

        Member member = new Member();
        MemberHome h=new MemberHome();
        member = h.findById(memberid);
//        h.session.close();
        System.out.println(member.getMemberId());

        newPost.setMember(member); //member


        System.out.println(member.getFname());
        System.out.println(newPost.getTitle());

        // now we add post to the database :
        PostOperationsUtility postUtil = new PostOperationsUtility();
        postUtil.addPost(newPost,threadId);


        //   request.getParameterMap().put("threadTitle", thread.getTitle());
        //   request.getParameterMap().put("threadBody", thread.getBody());
        //   request.getParameterMap().put("id", thread.getThreadId());
        System.out.println("after add post in dispatch action");

        //response.sendRedirect("/trial.do");

        return mapping.findForward(SUCCESS);

    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element :
     * ( <action parameter="method" .../> )
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        System.out.println("here iam in edit method in post dispatch action");
        int postId = ((beans.PostActionForm) form).getPostid(); //here we get the post id
      PostHome ph=new PostHome();
        
        Post oldPost =ph.findById(postId);
        ph.releaseConnection();
        
        Integer memberId = ((beans.PostActionForm) form).getMemberid();

        oldPost.setBody(((beans.PostActionForm) form).getPostBody()); //body

        oldPost.setLastModified(sqlDate);
        oldPost.setTitle(((beans.PostActionForm) form).getPostTitle()); 
        
    Integer  useridsession= (Integer) request.getSession().getAttribute("id") ;
               
    
               Integer me=oldPost.getMember().getMemberId();
     
               if(!Check_previlidge(useridsession, me))
               {
                   
                  return mapping.findForward("login");
               }
//        System.out.println("post title" + post.getTitle());
        PostOperationsUtility postUtil=new PostOperationsUtility();
        // now we add post to the database :
        postUtil.editPost(oldPost, memberId);
        System.out.println("after editing the post in edit method");
        int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
        request.setAttribute("pagenumber", pagenumber);
        return mapping.findForward(SUCCESS);
    }
    public boolean Check_previlidge(Integer user,Integer memid)
    {
        
        boolean priv=false;
        Member mem=new  MemberHome().findById(user);
        System.out.println(" member on session is "+user+"member on post is "+memid);
        String type=mem.getType();
        System.out.println(" type of user "+type);
        if(memid==user||type.equals("adm"))
        {
            priv=true;
        }
        System.out.println(" perv is"+priv);
        return priv;
    }
}
